﻿//Get just the number    
function extractNumber(obj, e, decimalPlaces, allowNegative, bNumberGrouping, bDollar) {
    var temp = obj.value;

    // check for arrow keys
    if (window.event) { key = e.keyCode; isCtrl = window.event.ctrlKey }
    else if (e.which) { key = e.which; isCtrl = e.ctrlKey; }

    //get current caret position - http://www.webdeveloper.com/forum/showthread.php?t=74982
    var carPos = 0;
    if (document.selection) {
        // IE Support
        obj.focus();
        var oSel = document.selection.createRange();
        oSel.moveStart('character', -obj.value.length);  // Move selection start to 0 position
        carPos = oSel.text.length;   // The caret position is selection length
        //caretPos(obj);
    }
    else if (obj.selectionStart || obj.selectionStart == '0') {
        carPos = obj.selectionStart;
    }

    var curCommas = temp.split(',').length - 1; //count of current # of commas
    if (key == 9 || key == 37 || key == 38 || key == 39 || key == 40) return true;
    //if (key == 8 || isCtrl) return true;
    if (isCtrl) return true;

    // avoid changing things if already formatted correctly
    var reg0Str = '[0-9]*';
    if (decimalPlaces > 0) {
        reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
    } else if (decimalPlaces < 0) {
        reg0Str += '\\.?[0-9]*';
    }
    reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
    reg0Str = reg0Str + '$';
    var reg0 = new RegExp(reg0Str);

    //Format the number
    temp = FormatNumber(temp, bNumberGrouping, bDollar)

    if (reg0.test(temp)) return true;

    // first replace all non numbers
    var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
    var reg1 = new RegExp(reg1Str, 'g');
    temp = temp.replace(reg1, '');

    if (allowNegative) {    // replace extra negative
        var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
        var reg2 = /-/g;
        temp = temp.replace(reg2, '');
        if (hasNegative) temp = '-' + temp;
    }

    if (decimalPlaces != 0) {
        var reg3 = /\./g;
        var reg3Array = reg3.exec(temp);
        if (reg3Array != null) { // keep only first occurrence of . and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
            var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
            reg3Right = reg3Right.replace(reg3, '');
            reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
            temp = temp.substring(0, reg3Array.index) + '.' + reg3Right;
        }
    }

    //Format the number - NOT SURE WHY WE'RE FORMATTING AGAIN
    temp = FormatNumber(temp, bNumberGrouping, bDollar)

    obj.value = temp;

    var newCommas = temp.split(',').length - 1; //count of current # of commas
    if (bDollar) carPos += 1;   //move the car one more to the right if we have $    
    setCaretPosition(obj, carPos + (newCommas - curCommas));
}

function blockNonNumbers(obj, e, allowDecimal, allowNegative, bNumberGrouping, bDollar) {
    var key;
    var isCtrl = false;
    var keychar;
    var reg;

    if (window.event) { key = e.keyCode; isCtrl = window.event.ctrlKey }
    else if (e.which) { key = e.which; isCtrl = e.ctrlKey; }
    if (key == 9 || key == 37 || key == 38 || key == 39 || key == 40) return true; //Allow tab and arrow keys
    if (isNaN(key)) return true;
    keychar = String.fromCharCode(key);
    if (key == 8 || isCtrl) { return true; }  // check for backspace or delete, or if Ctrl was pressed
    if (bNumberGrouping == true) { if (key == 130) return true; } //allow comma if numbergrouping is allowed

    reg = /\d/;
    var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
    var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
    return isFirstN || isFirstD || reg.test(keychar);
}

//Add Commas and $ to the number
function FormatNumber(num, bNumberGrouping, bDollar) {
    if (bNumberGrouping == true) {
        var re = /(-?\d+)(\d{3})/;

        while (re.test(num)) {
            num = num.replace(re, '$1,$2')
        }
    }

    if (bDollar == true) {
        num.replace('$', '');
        num = '$' + num;
    }
    return num;
}

//Remove Commas
function decommafy(obj) {
    var re = /,/g;
    obj.value = obj.value.replace(re, '');
}

//Get the current caret position
function caretPos(ctrl) {
    var CaretPos = 0;
    if (document.selection) {
        ctrl.focus();
        var Sel = document.selection.createRange();
        Sel.moveStart('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
        return CaretPos
    }
}

//Set the caret position for editing
function setCaretPosition(ctrl, pos) {
    // IE Support
    if (document.selection) {
        //ie support
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
    else {
        //Firefox        
        ctrl.selectionStart = pos;
        ctrl.selectionEnd = pos;
        ctrl.focus();

        //ctrl.focus();
        //ctrl.setSelectionRange(pos, pos);

    }

}